#ifndef CONN_H
#define CONN_H

#ifdef WX_PRECOMP
#include "wx/wx.h"
#endif

#include <map>
#include <vector>

#include <wx/string.h>
#include <wx/socket.h>
#include <wx/thread.h>

#include "tux.h"


class MsnCommand;
class MsnCommandWrapper;
class MsnMessenger;

class MsnTransport;

class MsnConnection
{
friend class MsnTransport;

protected:
	MsnTransport* transport;

    time_t m_LastActive;

protected:

	//NS or SB
	wxString serverType;

	wxString userPassport;
    wxString userPassword;

	int connectionType;

    wxString serverAddress;
	wxString retryServerAddress;
    int serverPort;
   
	wxString proxyAddress;
	int proxyPort;

	wxString proxyUsername;
	wxString proxyPassword;

	bool closed;

public:
    MsnConnection();
    virtual ~MsnConnection();

	void SetConnectionType(int type){connectionType = type;}
	int GetConnectionType(){return connectionType;}

	void SetServerAddress(const wxString& server){serverAddress = server;}
	void SetServerPort(int port){serverPort = port;}

    void SetUserPassport(const wxString& passport){userPassport = passport;}
    void SetUserPassword(const wxString& password){userPassword = password;}

    wxString GetUserPassport(){return userPassport;}
    wxString GetUserPassword(){return userPassword;}

    wxString GetServerAddress(){return serverAddress;}

	void SetRetryServerAddress(const wxString& address){retryServerAddress = address;}

	void SetProxyAddress(const wxString& address){proxyAddress = address;}
	void SetProxyPort(int port){proxyPort = port;}

	void SetProxyUsername(const wxString& username){proxyUsername = username;}
	void SetProxyPassword(const wxString& password){proxyPassword = password;}

	wxString GetProxyAddress(){return proxyAddress;}
	int GetProxyPort(){return proxyPort;}

	wxString GetProxyUsername(){return proxyUsername;}
	wxString GetProxyPassword(){return proxyPassword;}

    time_t GetLastActive(){return m_LastActive;}


public:
	//open the transport
    void Open();

	//close the transport, the thread might not exit immediately
    void Close();

	//this method is used to destroy the transport thread. SHOULD NOT be called from the transport thread context
	void Destroy();

	void ReConnect(const wxString& address, int port);

	bool IsConnected();


    void SendCommand(MsnCommand& cmd);
    void SendCommand(const wxString& cmd);
    void SendCommand(MsnCommand& cmd, const wxString& content);

/*    
	void SendString(const wxString& msg);

    void SendBuffer(wxMemoryBuffer& buf);

	void SendChunk(char* chunk, unsigned int size);

	void ReadChunk(char* buf, unsigned int len);
*/


public:

    virtual void ConnectionOpened() = 0;
    virtual void ConnectionFailed() = 0;
    virtual void ConnectionClosed() = 0;
    virtual void ConnectionBroken() = 0;

    //virtual void ProcessCommand(const wxString& line) = 0; 

	virtual void ProcessCommand(MsnCommandWrapper* cmdWrapper) = 0; 

};

#endif
